# -*- coding: utf-8 -*-
"""
@Time: 2021/12/13 14:31
@Author: jins
@File: table_electricts.py
@Introduction: electricts表造数据
"""

from common.mysqlDataOperate import OperateMysql
import random
from datetime import datetime, timedelta


def insert(start):
    op = OperateMysql()
    id = 1
    for j in range(start + 1, start + 3):
        data_time = datetime(2020, 11, 1, 0, 0, 0)
        IndicatedFApA = 500.0  # A相正向有功示值原始值(千瓦时)
        IndicatedFApB = 300.0  # B相正向有功示值原始值(千瓦时)
        IndicatedFApC = 100.0  # C相正向有功示值原始值(千瓦时)
        IndicatedFRpT = 200.0  # 正向无功总示值原始值(千乏时)
        IndicatedRApA = 200.0  # A相反向有功示值原始值(千瓦时)
        IndicatedRApB = 100.0  # B相反向有功示值原始值(千瓦时)
        IndicatedRApC = 50.0  # C相反向有功示值原始值(千瓦时)
        IndicatedRRpT = 20.0  # 反向无功总示值原始值(千乏时)
        IndicatedRp1T = 200.0  # 一象限无功示值原始值(千乏时)
        IndicatedRp2T = 150.0  # 二象限无功示值原始值(千乏时)
        IndicatedRp3T = 100.0  # 三象限无功示值原始值(千乏时)
        IndicatedRp4T = 50.0  # 四象限无功示值原始值(千乏时)
        ElectricityFApA = 3000.0  # A相正向有功电量原始值(千瓦时)
        ElectricityFApB = 1500.0  # B相正向有功电量原始值(千瓦时)
        ElectricityFApC = 750.0  # C相正向有功电量原始值(千瓦时)
        ElectricityFRpT = 500.0  # 正向无功总电量原始值(千乏时)
        ElectricityRApA = 500.0  # A相反向有功电量原始值(千瓦时)
        ElectricityRApB = 300.0  # B相反向有功电量原始值(千瓦时)
        ElectricityRApC = 150.0  # C相反向有功电量原始值(千瓦时)
        ElectricityRRpT = 250.0  # 反向无功总电量原始值(千瓦时)

        for i in range(2880):
            device_id = 10000000 + j
            IndicatedFApT = IndicatedFApA + IndicatedFApB + IndicatedFApC  # 正向有功总示值原始值(千瓦时)
            IndicatedRApT = IndicatedRApA + IndicatedRApB + IndicatedRApC  # 反向有功总示值原始值(千瓦时)
            ElectricityFApT = ElectricityFApA + ElectricityFApB + ElectricityFApC  # 正向有功总电量原始值(千瓦时)
            ElectricityRApT = ElectricityRApA + ElectricityRApB + ElectricityRApC  # 反向有功总电量原始值(千瓦时)
            PowerApA = random.uniform(1500.0, 2000.0)  # A相有功功率原始值(千瓦)
            PowerApB = random.uniform(1500.0, 2000.0)  # B相有功功率原始值(千瓦)
            PowerApC = random.uniform(1500.0, 2000.0)  # C相有功功率原始值(千瓦)
            PowerApT = PowerApA + PowerApB + PowerApC  # 总有功功率原始值(千瓦)
            PowerRpA = random.uniform(1500.0, 2000.0)  # A相无功功率原始值(千乏)
            PowerRpB = random.uniform(1500.0, 2000.0)  # B相无功功率原始值(千乏)
            PowerRpC = random.uniform(1500.0, 2000.0)  # C相无功功率原始值(千乏)
            PowerRpT = PowerRpA + PowerRpB + PowerRpC  # 总无功功率原始值(千乏)
            PowerFactorA = random.uniform(15.0, 50.0)  # A相功率因数原始值(无单位)
            PowerFactorB = random.uniform(15.0, 50.0)  # B相功率因数原始值(无单位)
            PowerFactorC = random.uniform(15.0, 50.0)  # C相功率因数原始值(无单位)
            PowerFactorT = PowerFactorA + PowerFactorB + PowerFactorC  # 总功率因数原始值(无单位)
            VoltageA = random.uniform(200.0, 250.0)  # A相电压原始值(伏特)
            VoltageB = random.uniform(200.0, 250.0)  # B相电压原始值(伏特)
            VoltageC = random.uniform(200.0, 250.0)  # C相电压原始值(伏特)
            CurrentA = random.uniform(5.0, 30.0)  # A相电流原始值(安培)
            CurrentB = random.uniform(5.0, 30.0)  # B相电流原始值(安培)
            CurrentC = random.uniform(5.0, 30.0)  # C相电流原始值(安培)
            CurrentZ = random.uniform(5.0, 30.0)  # 零相电流原始值(安培)
            VoltageAngleA = random.uniform(1.0, 10.0)  # A相电压相位角原始值(度)
            VoltageAngleB = random.uniform(1.0, 10.0)  # B相电压相位角原始值(度)
            VoltageAngleC = random.uniform(1.0, 10.0)  # C相电压相位角原始值(度)
            CurrentAngleA = random.uniform(1.0, 10.0)  # A相电流相位角原始值(度)
            CurrentAngleB = random.uniform(1.0, 10.0)  # B相电流相位角原始值(度)
            CurrentAngleC = random.uniform(1.0, 10.0)  # C相电流相位角原始值(度)
            TempA = random.uniform(10.0, 50.0)  # A相温度原始值(摄氏度)
            TempB = random.uniform(10.0, 50.0)  # B相温度原始值(摄氏度)
            TempC = random.uniform(10.0, 50.0)  # C相温度原始值(摄氏度)
            powerMpA = random.uniform(1500.0, 2000.0)  # A相分钟平均有功功率原始值(千瓦)
            powerMpB = random.uniform(1500.0, 2000.0)  # B相分钟平均有功功率原始值(千瓦)
            powerMpC = random.uniform(1500.0, 2000.0)  # C相分钟平均有功功率原始值(千瓦)
            RealDemandFApT = random.uniform(100.0, 2000.0)  # 正向有功需量原始值(千瓦)
            increase_num = random.uniform(5.0, 10.0)
            UpdateTime = data_time.timestamp() * 1000
            sql = f"""INSERT INTO xlcloud.electricts20b
            VALUES
                (   
                    {id},
                    {device_id},
                    '{data_time}',
                    {IndicatedFApT},
                    {IndicatedFApA},
                    {IndicatedFApB},
                    {IndicatedFApC},
                    {IndicatedFRpT},
                    {IndicatedRApT},
                    {IndicatedRApA},
                    {IndicatedRApB},
                    {IndicatedRApC},
                    {IndicatedRRpT},
                    {IndicatedRp1T},
                    {IndicatedRp2T},
                    {IndicatedRp3T},
                    {IndicatedRp4T},
                    {ElectricityFApT},
                    {ElectricityFApA},
                    {ElectricityFApB},
                    {ElectricityFApC},
                    {ElectricityFRpT},
                    {ElectricityRApT},
                    {ElectricityRApA},
                    {ElectricityRApB},
                    {ElectricityRApC},
                    {ElectricityRRpT},
                    {PowerApT},
                    {PowerApA},
                    {PowerApB},
                    {PowerApC},
                    {PowerRpT},
                    {PowerRpA},
                    {PowerRpB},
                    {PowerRpC},
                    {PowerFactorT},
                    {PowerFactorA},
                    {PowerFactorB},
                    {PowerFactorC},
                    {VoltageA},
                    {VoltageB},
                    {VoltageC},
                    {CurrentA},
                    {CurrentB},
                    {CurrentC},
                    {CurrentZ},
                    {VoltageAngleA},
                    {VoltageAngleB},
                    {VoltageAngleC},
                    {CurrentAngleA},
                    {CurrentAngleB},
                    {CurrentAngleC},
                    {TempA},
                    {TempB},
                    {TempC},
                    {powerMpA},
                    {powerMpB},
                    {powerMpC},
                    {RealDemandFApT},
                    1,
                    1,
                    {UpdateTime}
                    )
                    """
            op.batch_excute(sql)
            IndicatedFApA += increase_num
            IndicatedFApB += increase_num
            IndicatedFApC += increase_num
            IndicatedFRpT += increase_num
            IndicatedFApA += increase_num
            IndicatedRApB += increase_num
            IndicatedRApC += increase_num
            IndicatedRRpT += increase_num
            IndicatedRp1T += increase_num
            IndicatedRp2T += increase_num
            IndicatedRp3T += increase_num
            IndicatedRp4T += increase_num
            ElectricityFApA += increase_num
            ElectricityFApB += increase_num
            ElectricityFApC += increase_num
            ElectricityFRpT += increase_num
            ElectricityRApA += increase_num
            ElectricityRApB += increase_num
            ElectricityRApC += increase_num
            ElectricityRRpT += increase_num
            IndicatedRApA += increase_num
            data_time += timedelta(minutes=15)
            id += 1
        op.commit()
    op.close()


if __name__ == '__main__':
    insert(0)
